package com.g2room.tools.mybatisTools;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.g2room.tools.db.db2po.StringUtil;
import com.g2room.tools.mybatisTools.to.XmlMapper;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * 使用Freemarker模板生成文件
 * @author Administrator
 *
 */
public class CreateFileByFtl {
	// 保存文件路径
	private static String xmlFilePath = "D:/workspace/code/eclipse/stargame/carry6/adps/src/main/resources/";
	private static String basePackage= "D:/workspace/code/eclipse/stargame/carry6/adps/src/java/main/com/carry6/adps/";
	
	public static void main(String[] args) throws IOException{
		List<XmlMapper> xmlMappers = TableInfo.getTableInfo();
		
         // 进行排序处理
//		 Collections.sort(xmlMappers);
		 Configuration cfg = new Configuration();
		 cfg.setDirectoryForTemplateLoading(new File( "D:/workspace/code/eclipse/JavaProject/src/com/g2room/tools/mybatisTools/tmpl"));  
        
		 createMybatisXmlMapper(xmlMappers, cfg);
//		 createMybatisCfg(xmlMappers, cfg);
		 createMapper(xmlMappers, cfg);
		 createService(xmlMappers, cfg);
    }
	
	public static void createMybatisXmlMapper(List<XmlMapper> xmlMappers, Configuration cfg) {
        try {
            Template tmpl = cfg.getTemplate("GenMapper.xml");
            for(XmlMapper xmlMapper : xmlMappers) {
            	StringWriter out = null;
	            try {
	            	out = new StringWriter();
	            	tmpl.process(xmlMapper, out);
	            	// 生成Mapper文件
					createFile(out.toString(), xmlFilePath, "mybatis/" + xmlMapper.getXmlName() + ".xml");
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						if(null != out) out.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
            }
        } catch (IOException e) {
        	e.printStackTrace();
        }
	}
	
	public static void createMybatisCfg(List<XmlMapper> xmlMappers, Configuration cfg) {
        // 生成配置文件
        try {
            Template tmpl = cfg.getTemplate("mybatis-config.xml");
	            StringWriter cfgTmplOut = null;
	            try {
	            	cfgTmplOut = new StringWriter();
	            	Map<String, List<XmlMapper>> xmlMapperMap = new HashMap<String, List<XmlMapper>>();
	            	xmlMapperMap.put("xmlCfg", xmlMappers);
	            	tmpl.process(xmlMapperMap, cfgTmplOut);
	            	// 生成Mybatis Config文件
	            	createFile(cfgTmplOut.toString(), xmlFilePath, "mybatis-config.xml");
	            } catch (Exception e) {
	            	e.printStackTrace();
	            } finally {
					try {
						if(null != cfgTmplOut) cfgTmplOut.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
        } catch (IOException e) {
        	e.printStackTrace();
        }
	}
	
	/**
	 * 创建Mapper的Java文件
	 * @param xmlMappers
	 * @param cfg
	 */
	public static void createMapper(List<XmlMapper> xmlMappers, Configuration cfg) {
        // 生成配置文件
        try {
            Template tmpl = cfg.getTemplate("GenMapper.tmpl");
            for(XmlMapper xmlMapper : xmlMappers) {
            	StringWriter out = null;
	            try {
	            	out = new StringWriter();
	            	tmpl.process(xmlMapper, out);
	            	// 生成Mapper文件
					createFile(out.toString(), basePackage, "mapper/" + xmlMapper.getXmlName() + "Mapper.java");
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						if(null != out) out.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
            }
        } catch (IOException e) {
        	e.printStackTrace();
        }
	}
	
	/**
	 * 创建Mapper的Java文件
	 * @param xmlMappers
	 * @param cfg
	 */
	public static void createService(List<XmlMapper> xmlMappers, Configuration cfg) {
		// 生成配置文件
		try {
			Template tmpl = cfg.getTemplate("GenService.tmpl");
			for(XmlMapper xmlMapper : xmlMappers) {
				StringWriter out = null;
				try {
					out = new StringWriter();
					tmpl.process(xmlMapper, out);
					// 生成Mapper文件
					createFile(out.toString(), basePackage, "service/" + xmlMapper.getXmlName() + "Service.java");
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						if(null != out) out.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void createFile(String content, String filePath, String fileName) throws Exception {
        //得到写文件路径
        File file = new File(filePath);
        //如果目录不存在，则创建目录
        if(!file.exists())  file.mkdirs();
        //写文件
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(filePath + "//" + fileName),"utf-8");
        out.write(content);
        out.flush();
        out.close();
    }
	
}
